//
//  loginVC.m
//  BaseProject
//
//  Created by hundredsandc on 2017/10/28.
//  Copyright © 2017年 hundredsandc. All rights reserved.
//

#import "loginVC.h"
#import "customTextField.h"
#import "registerVC.h"

@interface loginVC ()
@property (nonatomic,strong)customTextField *userNameTF;
@property (nonatomic,strong)customTextField *passWordTF;
@end

@implementation loginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    [self setupUI];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(resignTF:)];
    [self.view addGestureRecognizer:tap];
}
-(void)resignTF:(UITapGestureRecognizer *)tap{
    [_userNameTF resignFirstResponder];
    [_passWordTF resignFirstResponder];
}
-(void)setupUI{
    _userNameTF = [[customTextField alloc]initWithFrame:CGRectMake(25, 30, SCREEN_WIDTH-50, 44) leftImagePath:@"shoujihao" PlaceHolder:@"账号"];
    [self.view addSubview:_userNameTF];
    
    _passWordTF = [[customTextField alloc]initWithFrame:CGRectMake(25, _userNameTF.bottom+10, SCREEN_WIDTH-50, 44) leftImagePath:@"mima" PlaceHolder:@"密码"];
    _passWordTF.secureTextEntry = YES;
    [self.view addSubview:_passWordTF];
    
    UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn setTitleColor:RGBCOLOR(0x00, 0xbc, 0xd4) forState:UIControlStateNormal];
    registerBtn.frame = CGRectMake(SCREEN_WIDTH-50, _passWordTF.bottom+5, 25, 12);
    registerBtn.titleLabel.font = FONT(12);
    [registerBtn addTarget:self action:@selector(registerBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerBtn];
    UILabel *noAccount = [[UILabel alloc]initWithFrame:CGRectMake(registerBtn.left-55, registerBtn.top, 50, 12)];
    noAccount.font = FONT(12);
    noAccount.textColor = RGBCOLOR(0x99, 0x99, 0x99);
    noAccount.text = @"没有账号";
    [self.view addSubview:noAccount];
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setBackgroundImage:[UIImage imageNamed:@"denglu"] forState:UIControlStateNormal];
    loginBtn.frame = CGRectMake((SCREEN_WIDTH-80)/2, registerBtn.bottom+80, 80, 80);
    [loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
}
-(void)registerBtnClick{
    registerVC *vc = [registerVC new];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)loginBtnClick{
    if (_userNameTF.text == nil || [_userNameTF.text removeWhitespaceAndNewlinewithboolNewLine:YES].length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入用户名"];
        return;
    }
    if (_passWordTF.text.length < 6) {
        [SVProgressHUD showInfoWithStatus:@"密码至少6位"];
        return;
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"username"] = _userNameTF.text;
    params[@"password"] = _passWordTF.text;
    
    [HTTPManagers postForumUserWithURL:@"login" params:params success:^(id json) {
        if ([json[@"code"]integerValue]==0) {
            [UserDefault setObject:_userNameTF.text forKey:CurrentUser];
            [UserDefault synchronize];
            [SVProgressHUD showSuccessWithStatus:@"登陆成功" maskType:SVProgressHUDMaskTypeBlack];
            [self performSelector:@selector(pop) withObject:nil afterDelay:1];
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:@"登陆失败" maskType:SVProgressHUDMaskTypeBlack];
        }
    } failure:^(NSError *error) {
        
    }];
}
-(void)pop{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
